package com.example.mynewsweb.controller;

import com.example.mynewsweb.po.Comment;
import com.example.mynewsweb.po.New;
import com.example.mynewsweb.po.User;
import com.example.mynewsweb.service.AdminService;
import com.example.mynewsweb.service.CommentService;
import com.example.mynewsweb.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@Slf4j
@RequestMapping
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CommentService commentService;

    /*进行用户注册操作*/
    @PostMapping ("api/userRegister")
    public Map<String,Object> register(@RequestBody User user){
        log.info("当前登用户的信息: [{}]",user.toString());
        Map<String,Object> map=new HashMap<>();
        try{
            userService.register(user);
            User giveID=userService.findByName(user.getUserName());
            String userId=giveID.getUserId();
            map.put("state",true);
            map.put("msg","注册成功,你的账号为："+userId);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","错误提示："+e.getMessage());
            System.out.println("msg:"+e.getMessage());
        }
        return map;
    }

    /*用户登录*/
    @PostMapping("api/userLogin")
    public Map<String,Object>UserLogin(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        try{
            User userLog= userService.UserLogin(user);
            map.put("state",true);
            map.put("msg","登录成功");
            map.put("user",userLog);//拿到当前登录的用户的信息，传给前端
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","错误提示："+e.getMessage());
        }
        return  map;
    }

    /*普通用户申请成为创作者*/
    @PostMapping("api/applytoWriter")
    public Map<String,Object>applytoWriter(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        try{
            userService.applytoWriter(user);
            User updateUser=userService.findByID(user.getUserId());
            map.put("state",true);
            map.put("msg","已发送申请");
            map.put("user",updateUser);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","发送申请失败");
        }
        return map;
    }

    /*管理员空间：显示所有用户*/
    @ResponseBody
    @GetMapping("api/allUser")
    public List<User> allUsers(){
        return adminService.allUser();
    }

    /*管理员空间：显示提交申请的用户*/
    @ResponseBody
    @GetMapping("api/userApply")
    public List<User> userApply(){
        return adminService.apply();
    }

    /*管理员同意用户的申请*/
    @PostMapping("api/agreeApply")
    public Map<String,Object> agreeApply(@RequestBody User user){
        log.info("当前审批的用户: [{}]",user);
        Map<String,Object> map=new HashMap<>();
        try{
            adminService.agreeApply(user);
            map.put("state","true");
            map.put("msg","审批成功，已同意");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","false");
            map.put("msg","审批失败");
        }
        return map;
    }

    /*管理员驳回用户的申请*/
    @PostMapping("api/disagreeApply")
    public Map<String,Object> disagreeApply(@RequestBody User user){
        log.info("当前审批的用户: [{}]",user);
        Map<String,Object> map=new HashMap<>();
        try{
            adminService.disagreeApply(user);
            map.put("state","true");
            map.put("msg","审批成功，已驳回");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","false");
            map.put("msg","审批失败");
        }
        return map;
    }

    /*个人空间获取个人信息*/
    @ResponseBody
    @GetMapping("api/findByuserId")
    public User findByuserId(String userId){
        return userService.findByID(userId);
    }

    /*用户修改个人信息方案1*/
    @PostMapping ("api/updateUser1")
    public Map<String,Object> updateUser1(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        try{
            userService.updateUser1(user);
            User giveID=userService.findByID(user.getUserId());
            String userId=giveID.getUserId();
            map.put("state",true);
            map.put("msg","修改成功");
            map.put("user",giveID);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","错误提示："+e.getMessage());
            System.out.println("msg:"+e.getMessage());
        }
        return map;
    }

    /*用户修改个人信息方案2*/
    @PostMapping ("api/updateUser2")
    public Map<String,Object> updateUser2(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        try{
            User giveID=userService.findByID(user.getUserId());
            Comment comment=new Comment();
            comment.setUserId(user.getUserId());
            comment.setUserName(user.getUserName());
            userService.updateUser2(user);
            commentService.updateCommentUser(comment);
            User newUser = userService.findByID(user.getUserId());
            map.put("state",true);
            map.put("msg","修改成功");
            map.put("user",newUser);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","错误提示："+e.getMessage());
            System.out.println("msg:"+e.getMessage());
        }
        return map;
    }
}
